package com.example.restatr.Atcivity.Charity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Bean.CardBean;
import com.example.restatr.Atcivity.Charity.Bean.CharityHotBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

public class CharityAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private Banner rbt;
    private GridView card;
    private RecyclerView rv;
    private LinearLayout loading;
    private List<CharityHotBean.RowsBean> rowsBeans = new ArrayList<>();
    private List<CardBean> cardBeans=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_charity_at);
        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        rbt = findViewById(R.id.banner);
        card = findViewById(R.id.card);
        loading = findViewById(R.id.loading);
        rv = findViewById(R.id.rv);

        Tools.SetBarTitle(toolbar, barTitle, "爱心捐赠", search, this, false);
        Tools.SetBanner("/prod-api/api/public-welfare/ad-banner/list", MainActivity.sp.getString("token", ""), "data", "imgUrl",rbt,this);
        card.setNumColumns(4);
        Tools.SetCard("/prod-api/api/public-welfare/public-welfare-type/list", MainActivity.sp.getString("token",""),"url","data",
                "id","imgUrl","name",this,cardBeans,card);
        card.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(CharityAt.this, CharityListAt.class);
                intent.putExtra("id",cardBeans.get(position).getId());
                intent.putExtra("title",cardBeans.get(position).getTitle());
                startActivity(intent);
            }
        });
    }


    private void GetHotCharity() {
        rowsBeans.clear();
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/public-welfare/public-welfare-activity/recommend-list", MainActivity.sp.getString("token", ""));
            CharityHotBean charityHotBean = new Gson().fromJson(token, CharityHotBean.class);
            rowsBeans.addAll(charityHotBean.getRows());
            runOnUiThread(() -> {
                if (rowsBeans.size() > 0) {
                    loading.setVisibility(View.GONE);
                    rv.setAdapter(new adapter());
                }
            });
        }).start();
    }

    class adapter extends RecyclerView.Adapter<adapter.viewHolder> {
        class viewHolder extends RecyclerView.ViewHolder {
            private TextView title;
            private TextView labeled;
            private CardView introduction;
            private CardView type;
            private TextView typeName;
            private ImageView tp;
            private TextView date;
            private ImageView likeImage;
            private TextView likeNum;
            private ImageView readImg;
            private TextView ReadNum;
            private TextView commentNum;
            private ImageView commentImg;

            public viewHolder(@NonNull View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.title);
                labeled = itemView.findViewById(R.id.labeled);
                introduction = itemView.findViewById(R.id.introduction);
                type = itemView.findViewById(R.id.type);
                typeName = itemView.findViewById(R.id.typeName);
                tp = itemView.findViewById(R.id.tp);
                date = itemView.findViewById(R.id.date);
                likeImage = itemView.findViewById(R.id.likeImage);
                likeNum = itemView.findViewById(R.id.likeNum);
                readImg = itemView.findViewById(R.id.readImg);
                ReadNum = itemView.findViewById(R.id.ReadNum);
                commentNum = itemView.findViewById(R.id.commentNum);
                commentImg = itemView.findViewById(R.id.commentImg);

            }
        }

        @NonNull
        @Override
        public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate(R.layout.xw_card_item, parent, false);
            return new viewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull viewHolder holder, int position) {
            holder.title.setText(rowsBeans.get(position).getName());
            Glide.with(CharityAt.this)
                    .load(Tools.ip + rowsBeans.get(position).getImgUrl())
                    .transform(new RoundedCorners(15))
                    .into(holder.tp);
            holder.introduction.setVisibility(View.VISIBLE);
            holder.labeled.setText(rowsBeans.get(position).getAuthor());
            holder.type.setVisibility(View.VISIBLE);
            holder.typeName.setText(rowsBeans.get(position).getType().getName());
            holder.date.setText(rowsBeans.get(position).getActivityAt());
            holder.likeImage.setImageResource(R.drawable.ic_round_attach_money_24);
            holder.likeNum.setTextSize(12);
            holder.likeNum.setText(String.valueOf(rowsBeans.get(position).getMoneyTotal()));
            holder.readImg.setImageResource(R.drawable.ic_round_money_off_24);
            holder.ReadNum.setTextSize(12);
            holder.ReadNum.setText(String.valueOf(rowsBeans.get(position).getMoneyNow()));
            holder.commentImg.setImageResource(R.drawable.ic_round_supervisor_account_24);
            holder.commentNum.setTextSize(12);
            holder.commentNum.setText(String.valueOf(rowsBeans.get(position).getDonateCount()));
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(CharityAt.this, CharityInfoAt.class);
                    intent.putExtra("data",new Gson().toJson(rowsBeans.get(position)));
                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        GetHotCharity();
    }
}